/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package l1j.server.server.command.executor;

import java.util.StringTokenizer;

import l1j.opqlo.Server.L1World.L1World;
import l1j.server.server.datatables.IpTable;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_SystemMessage;

public class L1BanIp implements L1CommandExecutor {
	public static L1CommandExecutor getInstance() {
		return new L1BanIp();
	}

	private L1BanIp() {
	}

	@Override
	public void execute(final L1PcInstance pc, final String cmdName, final String arg) {
		try {
			final StringTokenizer stringtokenizer = new StringTokenizer(arg);
			//IPを指定
			final String s1 = stringtokenizer.nextToken();

			//add/delを指定(しなくてもOK)
			String s2 = null;
			try {
				s2 = stringtokenizer.nextToken();
			} catch (final Exception e) {
			}

			final IpTable iptable = IpTable.getInstance();
			final boolean isBanned = iptable.isBannedIp(s1);
			String host = null;//TODO //TODO ban_ipテーブルの不具合を修正
			final StringBuilder msg = new StringBuilder();

			msg.append("IP:").append(s1).append(" 連線中的角色名稱:\r\n");

			for (final L1PcInstance tg : L1World.getInstance().getAllPlayers()) {
				if (s1.equals(tg.getNetConnection().getIp().toString())) {
					msg.append(tg.getName()).append("\r\n");
					host = tg.getNetConnection().getIp().toString();//TODO
				}
			}

			if ("add".equalsIgnoreCase(s2) && !isBanned) {
				iptable.banIp(s1, host);//TODO BANリストへIPを加える
				msg.append("IP:").append(s1).append(" 被新增到封鎖名單。\r\n");

			} else if ("del".equalsIgnoreCase(s2) && isBanned) {
				if (iptable.liftBanIp(s1, host)) {//TODO BANリストからIPを削除する
					msg.append("IP:").append(s1).append(" 已從封鎖名單中刪除。\r\n");
				}
			} else {
				//BANの確認
				if (isBanned) {
					msg.append("IP:").append(s1).append(" 已被登記在封鎖名單中。\r\n");
				} else {
					msg.append("IP:").append(s1).append(" 尚未被登記在封鎖名單中。\r\n");
				}
			}
			if(pc!=null){
				pc.sendPackets(new S_SystemMessage(msg.toString()));
			}else{
				System.out.println(msg.toString());
			}
			msg.delete(0, msg.length());
		} catch (final Exception e) {
			if(pc!=null){
				pc.sendPackets(new S_SystemMessage(cmdName+ " IP [ add | del ]請輸入。"));
			}else{
				System.out.println(cmdName+ " IP [ add | del ]請輸入。");
			}
		}
	}
}
